#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>


#define m 128
#define n 128


	void b10chagen(int num,int base,int cb)		//十进制转n进制
{
	int i = 0;
	static int a[m];
	do
	{
		*(a+i) = num % cb;
		num = num / cb;
		i++;
	}while(num != 0);
	for(i--;i >= 0;i--)
	{
		if(*(a+i) > 9)
		printf("%c",(*(a+i)+'A'-10));
		else	
		printf("%d",*(a+i));
	}
	printf("\n");

}

	void b8chage10(int num,int base,int cbase)		//n进制转10进制
{
	static int k[m];
	int i = 0;
	int j = 0;

	do{
	k[i] = num % 10;
	num /= 10;
	i++;
	}while(num != 0);
	
	for(i = i--;i >= 0;i--)
	{
	switch(base)
	{
		case 8:	
		j += k[i]*pow(8,i);
		break;
		case 2:
		j += k[i]*pow(2,i);
		break;
	}
	}
	b10chagen(j,10,cbase);
}

	void b16chage10(char *a,int base,int cb,int len)
{

	char b[m] = {0};			//16转10;
	int k = 0;
	int i,t;
	int j = 0;


	for(i = 0;i < len;i++ )
	{
		if(a[i] >= '0' && a[i] <= '9')
			b[k++] = a[i] - '0';
		else if(a[i] > 9)
			b[k++] = a[i] + 10 - 'A';
	}
	for(i = 0,t = k-1;i < k;i++,t--)
	j += b[i]*pow(16,t);
	b10chagen(j,10,cb);	
}

	void func()
{
	int num,base,cb;
	char str[n] = {0};
	int j;

	printf("please input Initial binary system:\n");
	scanf("%d",&base);
	
	printf("Please input a number:\n");
	if(base == 16)
	scanf("%s",str);
	else
	scanf("%d",&num);
	
	
	printf("Please input system you want to change:\n");
	scanf("%d",&cb);

	int a = strlen(str);
	switch(base)
	{
		case 8:	case 2:
			b8chage10(num,base,cb);	
			break;
		case 10 :b10chagen(num,base,cb);
			break;
		case 16	:b16chage10(str,base,cb,a);
	}

}


	int main()
{
	func();
	exit(0);
}
